package com.lvmama.rhino.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.mapperDw.PriceGatherMapper;
import com.lvmama.rhino.mapperDw.PriceMapper;
import com.lvmama.rhino.pojo.PriceDetail;
import com.lvmama.rhino.pojo.PriceDetailQuery;
import com.lvmama.rhino.pojo.StockDetail;
import com.lvmama.rhino.service.PriceService;
import com.lvmama.rhino.utils.Constants;
import com.lvmama.rhino.utils.JSONConversionUtil;
import com.lvmama.rhino.utils.MD5Utils;
import com.lvmama.rhino.utils.PageInfoUtil;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

import java.util.*;

/**
 * @Author:caoyong
 * @Description:
 * @Date：2018/5/16
 */
@Service("PriceService")
public class PriceServiceImpl implements PriceService {
    private Logger log = Logger.getLogger(PriceServiceImpl.class);

    @Autowired
    private PriceMapper priceMapper;

    @Autowired
    private PriceGatherMapper priceGatherMapper;

    @Autowired
    JedisCluster jedisCluster;
    @Override
    public List<PriceDetail> selectPriceDetail(PriceDetailQuery query) {
        log.info("selectPriceDetail start, query:" + query);
        try {
            //如果是房型
            if (query.getGroupByBranch()){
               return priceMapper.selectAllPriceDetail(query);
            }
            return priceMapper.selectPriceDetail(query);
        } catch (Exception e) {
            log.error("selectPriceDetail error:" + e.getMessage(), e);
        }
        log.info("selectPriceDetail end.");
        return null;
    }

    @Override
    public PageInfo<PriceDetail> selectPriceDetailPage(PriceDetailQuery query) {
        log.info("selectPriceDetailPage start, query:" + ToStringBuilder.reflectionToString(query, ToStringStyle.DEFAULT_STYLE));
        //设置分页
        List<PriceDetail> PriceDetails = null;
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        try {
            PriceDetails = priceMapper.selectPriceDetail(query);
        } catch (Exception e) {
            log.error("selectPriceDetailPage error:" + e.getMessage(), e);
        }
        log.info("selectPriceDetailPage end.");
        return new PageInfo<>(PriceDetails);
    }

    @Override
    public PageInfo<PriceDetail> selectAllPriceDetailPage(PriceDetailQuery query) {
        log.info("selectAllPriceDetailPage start, query:" + ToStringBuilder.reflectionToString(query, ToStringStyle.DEFAULT_STYLE));
        //设置分页
        List<PriceDetail> PriceDetails = null;
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        try {
            PriceDetails = priceMapper.selectAllPriceDetail(query);
        } catch (Exception e) {
            log.error("selectAllPriceDetailPage error:" + e.getMessage(), e);
        }
        log.info("selectAllPriceDetailPage end.");
        return new PageInfo<>(PriceDetails);
    }

    @Override
    public List<PriceDetail> selectPriceGather(PriceDetailQuery query) {

        log.info("selectPriceGather start, query:" + query);
        String key = MD5Utils.encode(query.toString());
        if (!jedisCluster.exists(key)){
            List<PriceDetail> list = priceGatherMapper.selectPriceGather(query);
            jedisCluster.set(key, JSONConversionUtil.objToString(list));
            jedisCluster.expire(key,Constants.TIMEOUT);
        }
        String result = jedisCluster.get(key);
        log.info("selectPriceGather end.");
        return JSONConversionUtil.stringToList(result,PriceDetail.class);
    }

    @Override
    public PageInfoUtil<PriceDetail> selectPriceGatherPageByRedis(PriceDetailQuery query, int pageSize) {
        log.info("selectPriceGatherPageByRedis start, query:" + ToStringBuilder.reflectionToString(query, ToStringStyle.DEFAULT_STYLE));
        String key = MD5Utils.encode(query.toString());
        long total = jedisCluster.zcard(key);
        int pageStart = query.getCurrentPage() == 0 ? 0 : (query.getCurrentPage() - 1) * pageSize;
        int pageEnd = pageStart + pageSize - 1;
        //如果total 小于0 表示没有缓存重新加载
        if (total <= 0){
            List<PriceDetail>  priceDetails = priceGatherMapper.selectPriceGather(query);
            Map<String, Double> redisMap = new HashMap<>();
            if (priceDetails != null && priceDetails.size() > 0){
                double i = 0D;
                for (PriceDetail priceDetail : priceDetails){
                    if (priceDetail == null){
                        continue;
                    }
                    String result = JSONConversionUtil.objToString(priceDetail);
                    redisMap.put(result,i);
                    i ++;
                }
            }
            total = redisMap.size();
            jedisCluster.zadd(key, redisMap);
            jedisCluster.expire(key,Constants.TIMEOUT);

        }
        Set<String> resultSet = jedisCluster.zrange(key, pageStart, pageEnd);
        List<PriceDetail> list =  new ArrayList<>();

        if (resultSet != null && resultSet.size() > 0){
            for (String detail : resultSet){
                PriceDetail priceDetail = JSONConversionUtil.stringToObj(detail,PriceDetail.class);
                if (priceDetail != null){
                    list.add(priceDetail);
                }
            }
        }
        log.info("selectPriceGatherPageByRedis end.");
        return  new PageInfoUtil(list, query.getCurrentPage(), pageSize, total, pageStart, pageEnd);
    }


}
